import http from "./http.js";

// Home/index.js轮播图数据
export async function get_Home_Swipe(params = null) {
  let res = await http.get("/home/swiper", params);
  // 返回promise对象
  return res;
}

// Home/index.js 租房小组数据
export async function get_Home_Groups(params = null) {
  let res = await http.get("/home/groups", params);
  // 返回promise对象
  return res;
}

// Home/index.js 新闻资讯数据
export async function get_Home_News(params = null) {
  let res = await http.get("/home/news", params);
  // 返回promise对象
  return res;
}

// Home/Search.js 当前城市获取
const CUR_CITY = "cur_city";
export async function get_Current_City() {
  // 判断本地存储是否有当前城市信息
  let curcityString = localStorage.getItem(CUR_CITY);
  // console.log("curcityString=", curcityString);
  if (curcityString) {
    // 有：直接返回
    return JSON.parse(curcityString);
  }

  // 无：重新获取
  var myCity = new window.BMap.LocalCity();
  return new Promise((resolve, reject) => {
    myCity.get(async (result) => {
      // 根据城市获取相应的城市信息
      // let res = await get_Current_City({ name: result.name });
      let res = await http.get("/area/info", { name: result.name });

      // 添加本地存储
      let { label, value } = res.data.body;
      localStorage.setItem(CUR_CITY, JSON.stringify({ label, value }));
      // 返回promise对象
      // return res;
      resolve({ label, value });
    });
  });
}

// pages/citylist/index.js 获取城市列表数据
export async function get_City_List(params = { level: 1 }) {
  let res = await http.get("/area/city", params);
  // 返回promise对象
  return res;
}

//获取热门城市列表数据
export async function get_Hot_City() {
  let res = await http.get("/area/hot");
  // 返回promise对象
  return res;
}

// 获取地图房源数据
export async function get_Map_Hourse(
  params = { id: "AREA|e4940177-c04c-383d" }
) {
  let res = await http.get("/area/map", params);
  // 返回promise对象
  return res;
}

// 获取房屋信息
export async function get_Hourse_List(
  params = { cityId: "AREA|88cff55c-aaa4-e2e0" }
) {
  let res = await http.get("/houses", params);
  // 返回promise对象
  return res;
}

//  根据房屋id获取房屋详情信息
export async function get_Hourse_Detial(hourseid = "5cc4494549926d0e2b94c816") {
  let res = await http.get(`/houses/${hourseid}`);
  // 返回promise对象
  return res;
}
